package com.jiyinit.web.core.business.service;

import com.jiyinit.web.core.business.dao.TabCoreGroupDao;
import com.jiyinit.web.core.business.dao.TabCoreUserDao;
import com.jiyinit.web.core.business.dao.UserGroupDao;
import com.jiyinit.web.core.business.dto.MChatRequest;
import com.jiyinit.web.core.business.entity.TabCoreGroupEntity;
import com.jiyinit.web.core.business.entity.TabUserGroupEntity;
import com.jiyinit.web.core.business.enums.CommonEnum;
import com.jiyinit.web.core.business.vo.ContactsVo;
import com.jiyinit.web.core.business.vo.GroupMemberVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * Created by lr on 2017/12/8.
 */
@Service(value = "core.userGroupServiceImpl")
public class UserGroupServiceImpl {

    @Resource(name = "core.userGroupDao")
    private UserGroupDao userGroupDao;

    @Resource(name = "core.groupDao")
    private TabCoreGroupDao tabCoreGroupDao;

    @Resource(name = "core.tabCoreUserDao")
    private TabCoreUserDao tabCoreUserDao;

    public List<TabUserGroupEntity> findGroupMemberByGroupId(int groupId){
        return userGroupDao.findGroupMemberByGroupId(groupId);
    }

    public int addUsers(Integer groupId ,Integer[] userIds,Integer currrentUserId){
        int num=0;
        for(Integer userId: userIds){
            TabUserGroupEntity tabUserGroupEntity = new TabUserGroupEntity();
            tabUserGroupEntity.setGroupId(groupId);
            tabUserGroupEntity.setUserId(userId);
            tabUserGroupEntity.setCreateTime(new Date());
            tabUserGroupEntity.setCreateId(currrentUserId);
            tabUserGroupEntity.setIsDelete(CommonEnum.IsDelete.COMMON.getCode());
            tabUserGroupEntity.setIdentity(CommonEnum.Identity.GENGERAL.getCode());
            num+= userGroupDao.save(tabUserGroupEntity);
        }
        return num;
    }


    public HashMap<Integer,List<GroupMemberVo>> findGroupMember(Integer userId){
        List<TabCoreGroupEntity> list = tabCoreGroupDao.findGroupMember(userId);
        HashMap<Integer,List<GroupMemberVo>> users = new HashMap<>();
        for(TabCoreGroupEntity entity : list){
            Integer groupId = entity.getGroupId();
            List<GroupMemberVo> vo = tabCoreUserDao.findGroupMember(groupId);
            users.put(entity.getGroupId(),vo);
        }
        return users;
    }

    public List<TabCoreGroupEntity> findGroup(Integer userId) {
        List<TabCoreGroupEntity> list = tabCoreGroupDao.findGroupMember(userId);
        return list;
    }

    /**
     * 删除群组人员
     *
     */
    public int delUsers(TabUserGroupEntity userGroupEntity){
       TabUserGroupEntity list = new TabUserGroupEntity();
        list.setUserId(userGroupEntity.getUserId());
        list.setGroupId(userGroupEntity.getGroupId());
        list.setIsDelete(1);
        return userGroupDao.update(list);
    }

    /**
     * 获取进群时间
     * @param mChatRequest
     * @return
     */
    public List<TabUserGroupEntity> findBySelective(MChatRequest mChatRequest) {
        List<TabUserGroupEntity> list = userGroupDao.findBySelective(mChatRequest);
        return list;
    }

}
